using System;
using System.Collections;
using System.Data;
using System.Windows.Forms;
using C1.Win.C1TrueDBGrid;
using PCSComMaterials.Inventory.BO;
using PCSComSale.Order.BO;
using PCSComSale.Order.DS;
using PCSComUtils.Common;
using PCSComUtils.Common.BO;
using PCSComUtils.PCSExc;
using PCSUtils.Framework.ReportFrame;
using PCSUtils.Log;
using PCSUtils.Utils;
using CancelEventArgs = System.ComponentModel.CancelEventArgs;

namespace PCSSale.Order
{
    /// <summary>
    /// Summary description for SOQuote.
    /// </summary>
    public partial class SOQuote : Form
    {
        private DataTable _dtbGridLayOut;
        private DataSet _dstData;
        private DataSet _dtbBinCache;
        private bool _isShipped;
        private EnumAction _mFormMode;
        private readonly SOReleaseOrderBO _boRelease = new SOReleaseOrderBO();
        private readonly UtilsBO _boUtils = new UtilsBO();
        private readonly InventoryUtilsBO _boIvUtils = new InventoryUtilsBO();
        private const string ItemView = "v_ItemInSale";
        const string PaymenttermCode = "PAYMENTTERM_CODE";
        const string PartyCode = "PARTY_CODE";
        const string PartyName = "PARTY_NAME";
        const string PartyAdd = "PARTY_ADD";
        const string SaletypeCode = "SALETYPE_CODE";
        DateTime _dtmCurrentDate = DateTime.Now;

        public SOQuote()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
        }

        public const string This = "PCSSale.Order.SOQuote";

        #region private methods

        private void SwitchFormMode()
        {
            switch (_mFormMode)
            {
                case EnumAction.Add:
                    dtmTransDate.Enabled = true;
                    txtTransNo.Enabled = btnTransNo.Enabled = false;
                    txtOrderNo.Enabled = btnOrderNo.Enabled = true;
                    txtContact.Enabled = true;
                    txtTaxCode.Enabled = true;
                    dtmDeliveryDate.Enabled = true;
                    txtComment.Enabled = true;
                    txtLocation.Enabled = btnLocation.Enabled = true;
                    txtBin.Enabled = btnBin.Enabled = true;
                    txtBankAccount.ReadOnly = false;
                    btnBank.Enabled = true;

                    dgrdData.Splits[0].DisplayColumns[ITM_ProductTable.CODE_FLD].Button = true;
                    dgrdData.Splits[0].DisplayColumns[ITM_ProductTable.CODE_FLD].Locked = false;
                    dgrdData.Splits[0].DisplayColumns[ITM_ProductTable.DESCRIPTION_FLD].Button = true;
                    dgrdData.Splits[0].DisplayColumns[ITM_ProductTable.DESCRIPTION_FLD].Locked = false;
                    dgrdData.Splits[0].DisplayColumns[SO_CommitInventoryDetailTable.PRICE_FLD].Locked = false;

                    btnAdd.Enabled = false;
                    btnSave.Enabled = true;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnPrint.Enabled = false;
                    break;
                case EnumAction.Edit:
                    dtmTransDate.Enabled = true;
                    txtTransNo.Enabled = btnTransNo.Enabled = false;
                    txtOrderNo.Enabled = btnOrderNo.Enabled = false;
                    txtContact.Enabled = true;
                    txtTaxCode.Enabled = true;
                    dtmDeliveryDate.Enabled = true;
                    txtComment.Enabled = true;
                    txtLocation.Enabled = btnLocation.Enabled = false;
                    txtBin.Enabled = btnBin.Enabled = false;
                    txtBankAccount.ReadOnly = false;
                    btnBank.Enabled = true;

                    foreach (C1DisplayColumn c1Col in dgrdData.Splits[0].DisplayColumns)
                        c1Col.Locked = true;

                    btnAdd.Enabled = false;
                    btnSave.Enabled = true;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnPrint.Enabled = false;
                    break;
                default:
                    dtmTransDate.Enabled = false;
                    txtTransNo.Enabled = btnTransNo.Enabled = true;
                    txtOrderNo.Enabled = btnOrderNo.Enabled = false;
                    txtContact.Enabled = false;
                    txtTaxCode.Enabled = false;
                    dtmDeliveryDate.Enabled = false;
                    txtComment.Enabled = false;
                    txtLocation.Enabled = btnLocation.Enabled = false;
                    txtBin.Enabled = btnBin.Enabled = false;
                    txtBankAccount.ReadOnly = true;
                    btnBank.Enabled = false;

                    btnAdd.Enabled = true;
                    btnSave.Enabled = false;
                    if (txtTransNo.Tag != null)
                    {
                        btnEdit.Enabled = true;
                        btnDelete.Enabled = !_isShipped;
                        btnPrint.Enabled = true;
                    }
                    else
                    {
                        btnEdit.Enabled = false;
                        btnDelete.Enabled = false;
                        btnPrint.Enabled = false;
                    }
                    break;
            }
        }
        private void ClearForm(bool blnIsMain)
        {
            if (blnIsMain)
            {
                dtmTransDate.Value = null;
                txtTransNo.Text = string.Empty;
                txtTransNo.Tag = null;
                dtmDeliveryDate.Value = null;
                txtContact.Text = string.Empty;
                txtComment.Text = string.Empty;
                txtTaxCode.Text = string.Empty;
                txtLocation.Text = string.Empty;
                txtLocation.Tag = null;
                txtBin.Text = string.Empty;
                txtBin.Tag = null;
            }
            txtOrderNo.Text = string.Empty;
            txtOrderNo.Tag = null;
            dtmOrderDate.Value = null;
            txtCustomerCode.Text = string.Empty;
            txtCustomerName.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtSaleType.Text = string.Empty;
            txtPayTerm.Text = string.Empty;
            txtBankAccount.Text = string.Empty;
            txtBankAccount.Tag = null;
            txtBankName.Text = string.Empty;

            if (_dstData != null && _dstData.Tables.Count > 0)
                _dstData.Tables[0].Clear();
            if (_dtbBinCache != null && _dtbBinCache.Tables.Count > 0)
                _dtbBinCache.Tables[0].Clear();
        }
        private void LoadDataToGrid()
        {
            dgrdData.DataSource = _dstData.Tables[0];
            FormControlComponents.RestoreGridLayout(dgrdData, _dtbGridLayOut);

            foreach (C1DisplayColumn c1Col in dgrdData.Splits[0].DisplayColumns)
                c1Col.Locked = true;

            dgrdData.Columns[SO_CommitInventoryDetailTable.AVAILABLEQUANTITY_FLD].NumberFormat = Constants.DECIMAL_NUMBERFORMAT;
            dgrdData.Columns[SO_SaleOrderDetailTable.NETAMOUNT_FLD].NumberFormat = Constants.DECIMAL_NUMBERFORMAT;
            dgrdData.Columns[SO_CommitInventoryDetailTable.COMMITQUANTITY_FLD].NumberFormat = Constants.DECIMAL_NUMBERFORMAT;
            dgrdData.Columns[SO_CommitInventoryDetailTable.PRICE_FLD].NumberFormat = Constants.DECIMAL_NUMBERFORMAT;
            dgrdData.Columns[SO_SaleOrderDetailTable.VATPERCENT_FLD].NumberFormat = Constants.DECIMAL_NUMBERFORMAT;
            dgrdData.Columns[SO_SaleOrderDetailTable.VATAMOUNT_FLD].NumberFormat = Constants.DECIMAL_NUMBERFORMAT;
            dgrdData.Columns[SO_CommitInventoryDetailTable.SHIPPED_FLD].ValueItems.Presentation = PresentationEnum.CheckBox;
            dgrdData.Columns[SO_CommitInventoryDetailTable.SHIPDATE_FLD].NumberFormat = Constants.DATETIME_FORMAT_HOUR;

            SwitchFormMode();
        }
        private void FillItemDataToGrid(DataRow pdrowData)
        {
            if (pdrowData != null)
            {
                dgrdData.Columns[ITM_ProductTable.CODE_FLD].Value = pdrowData[ITM_ProductTable.CODE_FLD];
                dgrdData.Columns[ITM_ProductTable.DESCRIPTION_FLD].Value = pdrowData[ITM_ProductTable.DESCRIPTION_FLD];
                dgrdData.Columns[SO_CommitInventoryDetailTable.PRODUCTID_FLD].Value = pdrowData[ITM_ProductTable.PRODUCTID_FLD];
                dgrdData.Columns[SO_CommitInventoryDetailTable.LINE_FLD].Value = dgrdData.Row + 1;
                dgrdData.Columns["UM"].Value = pdrowData["UM"];
                dgrdData.Columns[SO_SaleOrderDetailTable.STOCKUMID_FLD].Value = pdrowData[SO_SaleOrderDetailTable.STOCKUMID_FLD];
                dgrdData.Columns[SO_SaleOrderDetailTable.SALEORDERDETAILID_FLD].Value = pdrowData[SO_SaleOrderDetailTable.SALEORDERDETAILID_FLD];
                dgrdData.Columns[SO_DeliveryScheduleTable.DELIVERYSCHEDULEID_FLD].Value = pdrowData[SO_DeliveryScheduleTable.DELIVERYSCHEDULEID_FLD];
                dgrdData.Columns[SO_CommitInventoryDetailTable.COMMITQUANTITY_FLD].Value = pdrowData[SO_DeliveryScheduleTable.DELIVERYQUANTITY_FLD];
                dgrdData.Columns[SO_CommitInventoryDetailTable.PRICE_FLD].Value = pdrowData[SO_SaleOrderDetailTable.UNITPRICE_FLD];
                dgrdData.Columns[SO_SaleOrderDetailTable.NETAMOUNT_FLD].Value = pdrowData[SO_SaleOrderDetailTable.NETAMOUNT_FLD];
                dgrdData.Columns[SO_SaleOrderDetailTable.VATPERCENT_FLD].Value = pdrowData[SO_SaleOrderDetailTable.VATPERCENT_FLD];
                dgrdData.Columns[SO_SaleOrderDetailTable.VATAMOUNT_FLD].Value = pdrowData[SO_SaleOrderDetailTable.VATAMOUNT_FLD];
            }
            else
            {
                dgrdData.Columns[ITM_ProductTable.CODE_FLD].Value = string.Empty;
                dgrdData.Columns[ITM_ProductTable.DESCRIPTION_FLD].Value = string.Empty;
                dgrdData.Columns[SO_CommitInventoryDetailTable.PRODUCTID_FLD].Value = null;
                dgrdData.Columns[SO_CommitInventoryDetailTable.LINE_FLD].Value = null;
                dgrdData.Columns["UM"].Value = string.Empty;
                dgrdData.Columns[SO_SaleOrderDetailTable.STOCKUMID_FLD].Value = null;
                dgrdData.Columns[SO_SaleOrderDetailTable.SALEORDERDETAILID_FLD].Value = null;
                dgrdData.Columns[SO_DeliveryScheduleTable.DELIVERYSCHEDULEID_FLD].Value = null;
                dgrdData.Columns[SO_CommitInventoryDetailTable.COMMITQUANTITY_FLD].Value = null;
                dgrdData.Columns[SO_CommitInventoryDetailTable.PRICE_FLD].Value = null;
                dgrdData.Columns[SO_SaleOrderDetailTable.NETAMOUNT_FLD].Value = null;
                dgrdData.Columns[SO_SaleOrderDetailTable.VATPERCENT_FLD].Value = null;
                dgrdData.Columns[SO_SaleOrderDetailTable.VATAMOUNT_FLD].Value = null;
            }
            dgrdData.Columns[SO_CommitInventoryDetailTable.SHIPPED_FLD].Value = false;
            dgrdData.Columns[SO_CommitInventoryDetailTable.SHIPDATE_FLD].Value = null;
        }

        private void FillSaleOrderData(int pintSaleOrderID)
        {
            if (pintSaleOrderID > 0)
            {
                DataRow drowMasterInfo = _boRelease.GetSOMasterVO(pintSaleOrderID);
                txtOrderNo.Text = drowMasterInfo[SO_SaleOrderMasterTable.CODE_FLD].ToString();
                txtOrderNo.Tag = pintSaleOrderID;
                btnOrderNo.Tag = drowMasterInfo[SO_SaleOrderMasterTable.SHIPFROMLOCID_FLD];
                dtmOrderDate.Value = Convert.ToDateTime(drowMasterInfo[SO_SaleOrderMasterTable.TRANSDATE_FLD]);
                txtCustomerCode.Text = drowMasterInfo[PartyCode].ToString();
                txtCustomerName.Text = drowMasterInfo[PartyName].ToString();
                txtSaleType.Text = drowMasterInfo[SaletypeCode].ToString();
                txtPayTerm.Text = drowMasterInfo[PaymenttermCode].ToString();
                txtAddress.Text = drowMasterInfo[PartyAdd].ToString();
                if (drowMasterInfo[MST_PartyTable.VATCODE_FLD] != DBNull.Value && drowMasterInfo[MST_PartyTable.VATCODE_FLD] != null)
                {
                    txtTaxCode.Text = drowMasterInfo[MST_PartyTable.VATCODE_FLD].ToString();
                }

                //load default data for grid
                _dstData = _boRelease.ListBySale(pintSaleOrderID);
                // fill available quantity
                int i = 0;
                foreach (DataRow dr in _dstData.Tables[0].Rows)
                    dr[SO_CommitInventoryDetailTable.LINE_FLD] = ++i;
                _dstData.Tables[0].Columns[SO_CommitInventoryDetailTable.CCNID_FLD].DefaultValue = drowMasterInfo[SO_SaleOrderMasterTable.CCNID_FLD];
                if (_dtbBinCache != null && _dtbBinCache.Tables.Count > 0 && _dtbBinCache.Tables[0].Rows.Count > 0)
                {
                    // fill available quantity
                    foreach (DataRow dr in _dstData.Tables[0].Rows)
                    {
                        if (dr.RowState == DataRowState.Deleted)
                            continue;
                        string strFilter = ITM_ProductTable.PRODUCTID_FLD + "=" + dr[ITM_ProductTable.PRODUCTID_FLD];
                        decimal decOHQuantity = 0, decCommitQuantity = 0;
                        try
                        {
                            decOHQuantity = Convert.ToDecimal(_dtbBinCache.Tables[0].Compute("SUM(" + IV_BinCacheTable.OHQUANTITY_FLD + ")", strFilter));
                        }
                        catch { }
                        try
                        {
                            decCommitQuantity = Convert.ToDecimal(_dtbBinCache.Tables[0].Compute("SUM(" + IV_BinCacheTable.COMMITQUANTITY_FLD + ")", strFilter));
                        }
                        catch { }
                        dr[SO_CommitInventoryDetailTable.AVAILABLEQUANTITY_FLD] = decOHQuantity - decCommitQuantity;
                        dr[SO_CommitInventoryDetailTable.LOCATIONID_FLD] = Convert.ToInt32(txtLocation.Tag);
                        dr[SO_CommitInventoryDetailTable.BINID_FLD] = Convert.ToInt32(txtBin.Tag);
                    }
                }
                LoadDataToGrid();
            }
        }
        private bool ValidateData()
        {
            #region check mandatory

            if (dtmTransDate.Value == DBNull.Value)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                dtmTransDate.Focus();
                return false;
            }
            if (txtTransNo.Text.Trim().Length == 0)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                txtTransNo.Focus();
                return false;
            }
            if (txtOrderNo.Text.Trim().Length == 0)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                txtOrderNo.Focus();
                return false;
            }
            if (dtmDeliveryDate.Value == DBNull.Value)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                dtmDeliveryDate.Focus();
                return false;
            }
            if (txtLocation.Text.Trim().Length == 0)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                txtLocation.Focus();
                return false;
            }
            if (txtBin.Text.Trim().Length == 0)
            {
                PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                txtBin.Focus();
                return false;
            }

            #endregion

            #region check grid
            for (int i = 0; i < dgrdData.RowCount - 1; i++)
            {
                // check product
                if (dgrdData[i, SO_CommitInventoryDetailTable.PRODUCTID_FLD].ToString() == string.Empty ||
                    dgrdData[i, SO_CommitInventoryDetailTable.PRODUCTID_FLD] == null)
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Error);
                    dgrdData.Row = i;
                    dgrdData.Col = dgrdData.Splits[0].DisplayColumns.IndexOf(dgrdData.Splits[0].DisplayColumns[ITM_ProductTable.CODE_FLD]);
                    dgrdData.Focus();
                    return false;
                }
                try
                {
                    if (Convert.ToDecimal(dgrdData[i, SO_CommitInventoryDetailTable.PRICE_FLD]) <= 0)
                    {
                        var strMsg = new[] { dgrdData.Columns[SO_CommitInventoryDetailTable.PRICE_FLD].Caption, "0" };
                        PCSMessageBox.Show(ErrorCode.MESSAGE_GREATER_THAN, MessageBoxIcon.Warning, strMsg);
                        dgrdData.Row = i;
                        dgrdData.Col = dgrdData.Splits[0].DisplayColumns.IndexOf(dgrdData.Splits[0].DisplayColumns[SO_CommitInventoryDetailTable.PRICE_FLD]);
                        dgrdData.Focus();
                        return false;
                    }
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_INVALID_NUMERIC, MessageBoxIcon.Warning);
                    dgrdData.Row = i;
                    dgrdData.Col = dgrdData.Splits[0].DisplayColumns.IndexOf(dgrdData.Splits[0].DisplayColumns[SO_CommitInventoryDetailTable.PRICE_FLD]);
                    dgrdData.Focus();
                    return false;
                }
                try
                {
                    decimal decCommitQuantity = Convert.ToDecimal(dgrdData[i, SO_CommitInventoryDetailTable.COMMITQUANTITY_FLD]);
                    decimal decAvailableQuantity = Convert.ToDecimal(Convert.ToDecimal(dgrdData[i, SO_CommitInventoryDetailTable.AVAILABLEQUANTITY_FLD]));
                    if (decCommitQuantity > decAvailableQuantity)
                    {
                        PCSMessageBox.Show(ErrorCode.MESSAGE_AVAILABLE_QTY_MUST_GREATER_THAN_ZERO, MessageBoxIcon.Warning);
                        dgrdData.Row = i;
                        dgrdData.Col = dgrdData.Splits[0].DisplayColumns.IndexOf(dgrdData.Splits[0].DisplayColumns[SO_CommitInventoryDetailTable.COMMITQUANTITY_FLD]);
                        dgrdData.Focus();
                        return false;
                    }
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_INVALID_NUMERIC, MessageBoxIcon.Error);
                    dgrdData.Row = i;
                    dgrdData.Col = dgrdData.Splits[0].DisplayColumns.IndexOf(dgrdData.Splits[0].DisplayColumns[SO_CommitInventoryDetailTable.COMMITQUANTITY_FLD]);
                    dgrdData.Focus();
                    return false;
                }
            }
            #endregion

            return true;
        }
        private void LoadDataFromMaster(int pintMasterID)
        {
            DataRow drowMasterInfo = _boRelease.GetMasterVO(pintMasterID);
            dtmTransDate.Value = Convert.ToDateTime(drowMasterInfo[SO_CommitInventoryMasterTable.COMMITDATE_FLD]);
            txtTransNo.Text = drowMasterInfo[SO_CommitInventoryMasterTable.COMMITMENTNO_FLD].ToString();
            txtTransNo.Tag = drowMasterInfo[SO_CommitInventoryMasterTable.COMMITINVENTORYMASTERID_FLD];
            txtOrderNo.Text = drowMasterInfo[SO_SaleOrderMasterTable.CODE_FLD].ToString();
            txtOrderNo.Tag = drowMasterInfo[SO_SaleOrderMasterTable.SALEORDERMASTERID_FLD];
            dtmOrderDate.Value = Convert.ToDateTime(drowMasterInfo[SO_SaleOrderMasterTable.TRANSDATE_FLD]);
            txtCustomerCode.Text = drowMasterInfo[PartyCode].ToString();
            txtCustomerName.Text = drowMasterInfo[PartyName].ToString();
            txtAddress.Text = drowMasterInfo[PartyAdd].ToString();
            txtSaleType.Text = drowMasterInfo[SaletypeCode].ToString();
            txtPayTerm.Text = drowMasterInfo[PaymenttermCode].ToString();
            txtContact.Text = drowMasterInfo[SO_CommitInventoryMasterTable.CONTACT_FLD].ToString();
            txtComment.Text = drowMasterInfo[SO_CommitInventoryMasterTable.COMMENT_FLD].ToString();
            dtmDeliveryDate.Value = Convert.ToDateTime(drowMasterInfo[SO_CommitInventoryMasterTable.DELIVERYDATE_FLD]);
            txtLocation.Text = drowMasterInfo["Location"].ToString();
            txtLocation.Tag = drowMasterInfo[MST_LocationTable.LOCATIONID_FLD];
            txtBin.Text = drowMasterInfo["Bin"].ToString();
            txtBin.Tag = drowMasterInfo[MST_BINTable.BINID_FLD];

            if (drowMasterInfo[MST_BankTable.BankID] != DBNull.Value && drowMasterInfo[MST_BankTable.BankID] != null)
            {
                txtBankAccount.Tag = drowMasterInfo[MST_BankTable.BankID];
                txtBankAccount.Text = drowMasterInfo[MST_BankTable.BankAccount].ToString();
                txtBankName.Text = drowMasterInfo[MST_BankTable.BankName].ToString();
            }
            else
            {
                txtBankAccount.Tag = null;
                txtBankAccount.Text = string.Empty;
                txtBankName.Text = string.Empty;
            }

            _dstData = _boRelease.List(pintMasterID);
            foreach (DataRow dr in _dstData.Tables[0].Rows)
            {
                try
                {
                    if (Convert.ToBoolean(dr[SO_CommitInventoryDetailTable.SHIPPED_FLD]))
                    {
                        _isShipped = true;
                        break;
                    }
                }
                catch { }
            }
            LoadDataToGrid();
        }
        private void FillLocationData(DataRow pdrowData)
        {
            if (pdrowData != null)
            {
                txtLocation.Text = pdrowData[MST_LocationTable.CODE_FLD].ToString();
                txtLocation.Tag = pdrowData[MST_LocationTable.LOCATIONID_FLD];
            }
            else
            {
                txtLocation.Text = string.Empty;
                txtLocation.Tag = null;
            }
            txtBin.Text = string.Empty;
            txtBin.Tag = null;
            _dtbBinCache = new DataSet();
        }
        private void FillBinData(DataRow pdrowData)
        {
            if (pdrowData != null)
            {
                txtBin.Text = pdrowData[MST_BINTable.CODE_FLD].ToString();
                txtBin.Tag = pdrowData[MST_BINTable.BINID_FLD];
                _dtbBinCache = _boIvUtils.ListAllBinCache(Convert.ToInt32(txtBin.Tag));
                if (_dtbBinCache.Tables.Count > 0 && _dtbBinCache.Tables[0].Rows.Count > 0)
                {
                    // fill available quantity
                    foreach (DataRow dr in _dstData.Tables[0].Rows)
                    {
                        if (dr.RowState == DataRowState.Deleted)
                            continue;
                        string strFilter = ITM_ProductTable.PRODUCTID_FLD + "=" + dr[ITM_ProductTable.PRODUCTID_FLD];
                        decimal decOHQuantity = 0, decCommitQuantity = 0;
                        try
                        {
                            decOHQuantity = Convert.ToDecimal(_dtbBinCache.Tables[0].Compute("SUM(" + IV_BinCacheTable.OHQUANTITY_FLD + ")", strFilter));
                        }
                        catch { }
                        try
                        {
                            decCommitQuantity = Convert.ToDecimal(_dtbBinCache.Tables[0].Compute("SUM(" + IV_BinCacheTable.COMMITQUANTITY_FLD + ")", strFilter));
                        }
                        catch { }
                        dr[SO_CommitInventoryDetailTable.AVAILABLEQUANTITY_FLD] = decOHQuantity - decCommitQuantity;
                        dr[SO_CommitInventoryDetailTable.LOCATIONID_FLD] = Convert.ToInt32(txtLocation.Tag);
                        dr[SO_CommitInventoryDetailTable.BINID_FLD] = Convert.ToInt32(txtBin.Tag);
                    }
                }
            }
            else
            {
                txtBin.Text = string.Empty;
                txtBin.Tag = null;
                _dtbBinCache = new DataSet();
            }
        }
        /// <summary>
        /// We now calculate data in client site first, then store all data to database at the end of process.
        /// </summary>
        /// <param name="pdstData">Detail Data</param>
        /// <param name="pintLocationID">Location</param>
        /// <param name="pintBinID">Bin</param>
        private int UpdateRelease(SO_CommitInventoryMasterVO pvoMaster, DataSet pdstData, int pintLocationID, int pintBinID)
        {
            const string METHOD_NAME = This + ".UpdateRelease()";

            DataSet dtbLocCache = null;
            DataSet dtbMasLocCache = null;
            // get transaction history table schema
            DataSet dstTransaction = null;

            if (_mFormMode == EnumAction.Add)
            {
                DateTime dtmServerDate = _boUtils.GetDBDate();
                DataTable dtbAvailable = _boIvUtils.GetAvailableQtyByPostDate(dtmServerDate);
                dtbLocCache = _boIvUtils.ListAllLocationCache(pintLocationID);
                dtbMasLocCache = _boIvUtils.ListAllMasLocCache();
                // get transaction history table schema
                dstTransaction = _boIvUtils.ListTransactionHistory(0);
                int intTranTypeID = _boUtils.GetTransTypeIDByCode(TransactionTypeEnum.SOCommitment.ToString());
                foreach (DataRow drowData in pdstData.Tables[0].Rows)
                {
                    if (drowData.RowState == DataRowState.Deleted)
                        continue;
                    int intMasterLocationID = SystemProperty.MasterLocationID;
                    // Update Inventory Cache
                    decimal decCommitQuantity = decimal.Parse(drowData[SO_CommitInventoryDetailTable.COMMITQUANTITY_FLD].ToString());
                    int intProductID = int.Parse(drowData[ITM_ProductTable.PRODUCTID_FLD].ToString());

                    #region check available quantity

                    string strFilter = IV_BinCacheTable.PRODUCTID_FLD + "=" + intProductID;
                    decimal decAvailQty = 0;
                    try
                    {
                        decAvailQty = Convert.ToDecimal(dtbAvailable.Compute("SUM(AvailableQuantity)", strFilter));
                    }
                    catch { }

                    if (decAvailQty < decCommitQuantity)
                    {
                        Hashtable hstIndex = new Hashtable();
                        hstIndex.Add(ITM_ProductTable.PRODUCTID_FLD, intProductID);
                        throw new PCSBOException(ErrorCode.MESSAGE_AVAILABLE_QTY_MUST_GREATER_THAN_ZERO, METHOD_NAME, new Exception(), hstIndex);
                    }

                    #endregion

                    #region Cache

                    DataRow[] drowBinCache = _dtbBinCache.Tables[0].Select(strFilter);
                    DataRow[] drowLocCache = dtbLocCache.Tables[0].Select(strFilter);
                    DataRow[] drowMasLocCache = dtbMasLocCache.Tables[0].Select(strFilter);
                    decimal decBinCommit = 0, decLocCommit = 0, decMasLocCommit = 0;
                    try
                    {
                        decBinCommit = Convert.ToDecimal(drowBinCache[0][IV_BinCacheTable.COMMITQUANTITY_FLD]);
                    }
                    catch { }
                    try
                    {
                        decLocCommit = Convert.ToDecimal(drowLocCache[0][IV_LocationCacheTable.COMMITQUANTITY_FLD]);
                    }
                    catch { }
                    try
                    {
                        decMasLocCommit = Convert.ToDecimal(drowMasLocCache[0][IV_MasLocCacheTable.COMMITQUANTITY_FLD]);
                    }
                    catch { }
                    if (drowBinCache.Length > 0)
                    {
                        drowBinCache[0][IV_BinCacheTable.COMMITQUANTITY_FLD] = decBinCommit + decCommitQuantity;
                    }
                    if (drowLocCache.Length > 0)
                    {
                        drowLocCache[0][IV_LocationCacheTable.COMMITQUANTITY_FLD] = decLocCommit + decCommitQuantity;
                    }
                    if (drowMasLocCache.Length > 0)
                    {
                        drowMasLocCache[0][IV_MasLocCacheTable.COMMITQUANTITY_FLD] = decMasLocCommit + decCommitQuantity;
                    }
                    #endregion

                    #region Transaction history
                    //SaveTransactionHistory
                    DataRow drowTransaction = dstTransaction.Tables[0].NewRow();
                    drowTransaction[MST_TransactionHistoryTable.CCNID_FLD] = drowData[ITM_ProductTable.CCNID_FLD];
                    drowTransaction[MST_TransactionHistoryTable.TRANSDATE_FLD] = DateTime.Now;
                    drowTransaction[MST_TransactionHistoryTable.POSTDATE_FLD] = dtmServerDate;
                    drowTransaction[MST_TransactionHistoryTable.REFMASTERID_FLD] = pvoMaster.CommitInventoryMasterID;
                    drowTransaction[MST_TransactionHistoryTable.REFDETAILID_FLD] = drowData[SO_CommitInventoryDetailTable.DELIVERYSCHEDULEID_FLD];
                    drowTransaction[MST_TransactionHistoryTable.PRODUCTID_FLD] = intProductID;
                    drowTransaction[MST_TransactionHistoryTable.TRANTYPEID_FLD] = intTranTypeID;
                    drowTransaction[MST_TransactionHistoryTable.USERNAME_FLD] = SystemProperty.UserName;
                    drowTransaction[MST_TransactionHistoryTable.QUANTITY_FLD] = decCommitQuantity;
                    drowTransaction[MST_TransactionHistoryTable.MASTERLOCATIONID_FLD] = intMasterLocationID;
                    try
                    {
                        drowTransaction[MST_TransactionHistoryTable.MASLOCOHQUANTITY_FLD] = Convert.ToDecimal(dtbMasLocCache.Tables[0].Compute("SUM(" + IV_MasLocCacheTable.OHQUANTITY_FLD + ")", strFilter));
                    }
                    catch { }
                    drowTransaction[MST_TransactionHistoryTable.MASLOCCOMMITQUANTITY_FLD] = decMasLocCommit;
                    drowTransaction[MST_TransactionHistoryTable.LOCATIONID_FLD] = pintLocationID;
                    try
                    {
                        drowTransaction[MST_TransactionHistoryTable.LOCATIONOHQUANTITY_FLD] = Convert.ToDecimal(dtbLocCache.Tables[0].Compute("SUM(" + IV_MasLocCacheTable.OHQUANTITY_FLD + ")", strFilter)); ;
                    }
                    catch { }
                    drowTransaction[MST_TransactionHistoryTable.LOCATIONCOMMITQUANTITY_FLD] = decLocCommit;
                    drowTransaction[MST_TransactionHistoryTable.BINID_FLD] = pintBinID;
                    try
                    {
                        drowTransaction[MST_TransactionHistoryTable.BINOHQUANTITY_FLD] = Convert.ToDecimal(_dtbBinCache.Tables[0].Compute("SUM(" + IV_MasLocCacheTable.OHQUANTITY_FLD + ")", strFilter)); ;
                    }
                    catch { }
                    drowTransaction[MST_TransactionHistoryTable.BINCOMMITQUANTITY_FLD] = decBinCommit;
                    drowTransaction[MST_TransactionHistoryTable.STOCKUMID_FLD] = drowData[ITM_ProductTable.STOCKUMID_FLD];
                    dstTransaction.Tables[0].Rows.Add(drowTransaction);
                    #endregion
                }
            }
            else
            {
                pvoMaster.CommitInventoryMasterID = Convert.ToInt32(txtTransNo.Tag);
            }
            return _boRelease.InsertOrUpDate(pvoMaster, _dstData, dtbMasLocCache, dtbLocCache, _dtbBinCache, dstTransaction, _mFormMode);
        }

        private void FillBankData(DataRowView row)
        {
            if (row != null)
            {
                txtBankAccount.Text = row[MST_BankTable.BankAccount].ToString();
                txtBankAccount.Tag = row[MST_BankTable.BankID];
                txtBankName.Text = row[MST_BankTable.BankName].ToString();
            }
            else
            {
                txtBankAccount.Text = string.Empty;
                txtBankAccount.Tag = null;
                txtBankName.Text = string.Empty;
            }
        }

        #endregion

        private void SOQuote_Load(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".SOQuote_Load()";
            try
            {
                Security objSecurity = new Security();
                this.Name = This;
                if (objSecurity.SetRightForUserOnForm(this, SystemProperty.UserName) == 0)
                {
                    this.Close();
                    PCSMessageBox.Show(ErrorCode.MESSAGE_YOU_HAVE_NO_RIGHT_TO_VIEW, MessageBoxIcon.Warning);
                    return;
                }

                //get grid layout
                _dtbGridLayOut = FormControlComponents.StoreGridLayout(dgrdData);

                _dstData = _boRelease.List(0);
                LoadDataToGrid();

                _mFormMode = EnumAction.Default;
                SwitchFormMode();

                _dtmCurrentDate = _boUtils.GetDBDate().AddDays(1);
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void SOQuote_Closing(object sender, CancelEventArgs e)
        {
            const string METHOD_NAME = This + ".SOQuote_Closing()";
            try
            {

            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnAdd_Click()";
            try
            {
                _mFormMode = EnumAction.Add;
                ClearForm(true);
                dtmTransDate.Value = _boUtils.GetDBDate();
                txtTransNo.Text = FormControlComponents.GetNoByMask(this);
                SwitchFormMode();
                txtOrderNo.Focus();
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnEdit_Click()";
            try
            {
                _mFormMode = EnumAction.Edit;
                SwitchFormMode();
                dtmTransDate.Focus();
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnDelete_Click()";
            try
            {
                if (_isShipped)
                    return;
                DialogResult dlgResult = PCSMessageBox.Show(ErrorCode.MESSAGE_DELETE_RECORD, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (dlgResult == DialogResult.Yes)
                {
                    _boRelease.Delete(Convert.ToInt32(txtTransNo.Tag), _dstData);

                    PCSMessageBox.Show(ErrorCode.MESSAGE_AFTER_SAVE_DATA);
                    _mFormMode = EnumAction.Default;
                    ClearForm(true);
                    SwitchFormMode();
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnPrint_Click()";
            const string REPORT_LAYOUT_FILE = "BaoGia.xml";
            const string APPLICATION_PATH = @"PCSMain\bin\Debug";
            const string REPORTFLD_TITLE = "fldTitle";
            try
            {
                this.Cursor = Cursors.WaitCursor;
                int intMasterID = 0;
                try
                {
                    intMasterID = Convert.ToInt32(txtTransNo.Tag);
                }
                catch
                {
                    return;
                }
                if (intMasterID <= 0)
                    return;
                DataTable dtbSource = _boRelease.GetReportData(intMasterID);
                C1PrintPreviewDialog printPreview = new C1PrintPreviewDialog();
                ReportBuilder objBuilder = new ReportBuilder();

                #region Get Actual Report Path
                //Get actual application path
                string strReportPath = Application.StartupPath;
                int intIndex = strReportPath.IndexOf(APPLICATION_PATH);
                if (intIndex > -1)
                {
                    strReportPath = strReportPath.Substring(0, intIndex);
                }

                if (strReportPath.Substring(strReportPath.Length - 1) == @"\")
                {
                    strReportPath += Constants.REPORT_DEFINITION_STORE_LOCATION;
                }
                else
                {
                    strReportPath += "\\" + Constants.REPORT_DEFINITION_STORE_LOCATION;
                }
                #endregion

                if (dtbSource != null)
                {
                    objBuilder.ReportName = "BaoGia";
                    objBuilder.SourceDataTable = dtbSource;
                }

                objBuilder.ReportLayoutFile = REPORT_LAYOUT_FILE;
                objBuilder.ReportDefinitionFolder = strReportPath;

                //check if layout is valid
                if (objBuilder.AnalyseLayoutFile())
                {
                    objBuilder.UseLayoutFile = true;
                }
                else
                {
                    PCSMessageBox.Show(ErrorCode.MESSAGE_REPORT_TEMPLATE_FILE_NOT_FOUND, MessageBoxIcon.Error);
                    return;
                }

                objBuilder.MakeDataTableForRender();

                // and show it in preview dialog
                objBuilder.ReportViewer = printPreview.ReportViewer;
                objBuilder.RenderReport();

                try
                {
                    decimal decAmount = Convert.ToDecimal(objBuilder.GetFieldByName("fldTotalAmount").Value);
                    string strAmountInWord = ConvertNumberToWord.ChuyenSoThanhChu(decAmount);
                    objBuilder.DrawPredefinedField("fldAmountInWord", strAmountInWord);
                }
                catch { }

                objBuilder.RefreshReport();

                //Print report
                try
                {
                    printPreview.FormTitle = objBuilder.GetFieldByName(REPORTFLD_TITLE).Text;
                }
                catch
                { }
                printPreview.Show();
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnHelp_Click()";
            try
            {

            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnClose_Click()";
            try
            {
                this.Close();
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnSave_Click()";
            DataSet dstTemp = null;
            try
            {
                if (Security.IsDifferencePrefix(this, lblTransNo, txtTransNo))
                    return;
                if (!dgrdData.EditActive && ValidateData())
                {
                    dgrdData.UpdateData();
                    dstTemp = _dstData.Copy();

                    // synchronyze data
                    FormControlComponents.SynchronyGridData(dgrdData);
                    SO_CommitInventoryMasterVO voMaster = new SO_CommitInventoryMasterVO();
                    voMaster.CommitDate = (DateTime)dtmTransDate.Value;
                    voMaster.CommitmentNo = txtTransNo.Text;
                    voMaster.Contact = txtContact.Text;
                    voMaster.TaxCode = txtTaxCode.Text;
                    voMaster.Comment = txtComment.Text;
                    voMaster.DeliveryDate = Convert.ToDateTime(dtmDeliveryDate.Value);
                    voMaster.EmployeeID = SystemProperty.EmployeeID;
                    voMaster.SaleOrderMasterID = Convert.ToInt32(txtOrderNo.Tag);
                    voMaster.Username = SystemProperty.UserName;
                    if (txtBankAccount.Tag != null)
                    {
                        voMaster.BankID = (int)txtBankAccount.Tag;
                    }

                    voMaster.CommitInventoryMasterID = UpdateRelease(voMaster, _dstData, Convert.ToInt32(txtLocation.Tag), Convert.ToInt32(txtBin.Tag));
                    if (_mFormMode == EnumAction.Add)
                        txtTransNo.Tag = voMaster.CommitInventoryMasterID;
                    PCSMessageBox.Show(ErrorCode.MESSAGE_AFTER_SAVE_DATA);
                    _mFormMode = EnumAction.Default;
                    SwitchFormMode();
                    txtTransNo.Focus();
                    //btnDelete.Enabled = true;
                }
            }
            catch (PCSException ex)
            {
                if (ex.mCode == ErrorCode.DUPLICATE_KEY)
                {
                    PCSMessageBox.Show(ErrorCode.DUPLICATE_KEY, MessageBoxIcon.Error);
                    txtTransNo.Focus();
                }
                else if (ex.mCode == ErrorCode.MESSAGE_AVAILABLE_QTY_MUST_GREATER_THAN_ZERO)
                {
                    PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                    _dstData = dstTemp.Copy();
                    dgrdData.Refresh();
                    string strErrorItem = ex.Hash[ITM_ProductTable.PRODUCTID_FLD].ToString();
                    for (int i = 0; i < dgrdData.RowCount; i++)
                    {
                        if (dgrdData[i, IV_MiscellaneousIssueDetailTable.PRODUCTID_FLD].ToString() == strErrorItem)
                        {
                            dgrdData.Row = i;
                            dgrdData.Col = dgrdData.Columns.IndexOf(dgrdData.Columns[ITM_ProductTable.CODE_FLD]);
                            dgrdData[i, SO_CommitInventoryDetailTable.AVAILABLEQUANTITY_FLD] = _boIvUtils.GetAvailableQtyByPostDate(_dtmCurrentDate, SystemProperty.CCNID, SystemProperty.MasterLocationID, (int)txtLocation.Tag, (int)txtBin.Tag, Convert.ToInt32(strErrorItem));
                            dgrdData.Focus();
                            _dstData = _dstData.Copy();
                            break;
                        }
                    }
                }
                else
                    PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void txtTransNo_Validating(object sender, CancelEventArgs e)
        {
            const string METHOD_NAME = This + ".txtTransNo_Validating()";
            try
            {
                if (txtTransNo.Modified && btnTransNo.Enabled)
                {
                    if (txtTransNo.Text.Trim().Equals(string.Empty))
                    {
                        ClearForm(true);
                        return;
                    }
                    else
                    {
                        DataRowView drvResult = FormControlComponents.OpenSearchForm(SO_CommitInventoryMasterTable.TABLE_NAME, SO_CommitInventoryMasterTable.COMMITMENTNO_FLD, txtTransNo.Text.Trim(), null, false);
                        if (drvResult != null)
                        {
                            txtTransNo.Text = drvResult[SO_CommitInventoryMasterTable.COMMITMENTNO_FLD].ToString();
                            txtTransNo.Tag = Convert.ToInt32(drvResult[SO_CommitInventoryMasterTable.COMMITINVENTORYMASTERID_FLD]);
                            LoadDataFromMaster(Convert.ToInt32(drvResult[SO_CommitInventoryMasterTable.COMMITINVENTORYMASTERID_FLD]));
                        }
                        else
                            e.Cancel = true;
                    }
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void txtTransNo_KeyDown(object sender, KeyEventArgs e)
        {
            const string METHOD_NAME = This + ".txtTransNo_KeyDown()";
            try
            {
                if (e.KeyCode == Keys.F4)
                    btnTransNo_Click(null, null);
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnTransNo_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnTransNo_Click()";
            try
            {
                DataRowView drvResult = FormControlComponents.OpenSearchForm(SO_CommitInventoryMasterTable.TABLE_NAME, SO_CommitInventoryMasterTable.COMMITMENTNO_FLD, txtTransNo.Text.Trim(), null, true);
                if (drvResult != null)
                {
                    txtTransNo.Text = drvResult[SO_CommitInventoryMasterTable.COMMITMENTNO_FLD].ToString();
                    txtTransNo.Tag = Convert.ToInt32(drvResult[SO_CommitInventoryMasterTable.COMMITINVENTORYMASTERID_FLD]);
                    LoadDataFromMaster(Convert.ToInt32(drvResult[SO_CommitInventoryMasterTable.COMMITINVENTORYMASTERID_FLD]));
                }
                else
                    txtTransNo.Focus();
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void txtOrderNo_Validating(object sender, CancelEventArgs e)
        {
            const string METHOD_NAME = This + ".txtOrderNo_Validating()";
            try
            {
                if (txtOrderNo.Modified && btnOrderNo.Enabled)
                {
                    if (txtOrderNo.Text.Trim().Equals(string.Empty))
                    {
                        ClearForm(false);
                        return;
                    }
                    else
                    {
                        DataRowView drvResult = FormControlComponents.OpenSearchForm(v_SOMasterToCommit.VIEW_NAME, SO_SaleOrderMasterTable.CODE_FLD, txtOrderNo.Text.Trim(), null, false);
                        if (drvResult != null)
                        {
                            txtOrderNo.Text = drvResult[SO_SaleOrderMasterTable.CODE_FLD].ToString();
                            txtOrderNo.Tag = Convert.ToInt32(drvResult[SO_SaleOrderMasterTable.SALEORDERMASTERID_FLD]);
                            FillSaleOrderData(Convert.ToInt32(txtOrderNo.Tag));
                        }
                        else
                            e.Cancel = true;
                    }
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void txtOrderNo_KeyDown(object sender, KeyEventArgs e)
        {
            const string METHOD_NAME = This + ".txtOrderNo_KeyDown()";
            try
            {
                if (e.KeyCode == Keys.F4)
                    btnOrderNo_Click(null, null);
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnOrderNo_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnOrderNo_Click()";
            try
            {
                DataRowView drvResult = FormControlComponents.OpenSearchForm(v_SOMasterToCommit.VIEW_NAME, SO_SaleOrderMasterTable.CODE_FLD, txtOrderNo.Text.Trim(), null, true);
                if (drvResult != null)
                {
                    txtOrderNo.Text = drvResult[SO_SaleOrderMasterTable.CODE_FLD].ToString();
                    txtOrderNo.Tag = Convert.ToInt32(drvResult[SO_SaleOrderMasterTable.SALEORDERMASTERID_FLD]);
                    FillSaleOrderData(Convert.ToInt32(txtOrderNo.Tag));
                }
                else
                    txtOrderNo.Focus();
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void txtLocation_Validating(object sender, CancelEventArgs e)
        {
            const string METHOD_NAME = This + ".txtLocation_Validating()";
            try
            {
                if (txtLocation.Modified && btnLocation.Enabled)
                {
                    if (txtLocation.Text.Trim().Equals(string.Empty))
                    {
                        FillLocationData(null);
                        return;
                    }
                    else
                    {
                        DataRowView drvResult = FormControlComponents.OpenSearchForm(MST_LocationTable.TABLE_NAME, MST_LocationTable.CODE_FLD, txtLocation.Text.Trim(), null, false);
                        if (drvResult != null)
                            FillLocationData(drvResult.Row);
                        else
                            e.Cancel = true;
                    }
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void txtLocation_KeyDown(object sender, KeyEventArgs e)
        {
            const string METHOD_NAME = This + ".txtLocation_KeyDown()";
            try
            {
                if (e.KeyCode == Keys.F4)
                    btnLocation_Click(null, null);
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnLocation_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnLocation_Click()";
            try
            {
                DataRowView drvResult = FormControlComponents.OpenSearchForm(MST_LocationTable.TABLE_NAME, MST_LocationTable.CODE_FLD, txtLocation.Text.Trim(), null, true);
                if (drvResult != null)
                    FillLocationData(drvResult.Row);
                else
                    txtTransNo.Focus();
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void txtBin_Validating(object sender, CancelEventArgs e)
        {
            const string METHOD_NAME = This + ".txtBin_Validating()";
            try
            {
                if (txtBin.Modified && btnBin.Enabled)
                {
                    if (txtBin.Text.Trim().Equals(string.Empty))
                    {
                        FillBinData(null);
                        return;
                    }
                    else
                    {
                        if (txtLocation.Text.Trim().Length == 0)
                        {
                            PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                            txtBin.Text = string.Empty;
                            txtLocation.Focus();
                            return;
                        }
                        Hashtable htCondition = new Hashtable();
                        htCondition.Add(MST_BINTable.LOCATIONID_FLD, Convert.ToInt32(txtLocation.Tag));
                        DataRowView drvResult = FormControlComponents.OpenSearchForm(MST_BINTable.TABLE_NAME, MST_BINTable.CODE_FLD, txtBin.Text.Trim(), htCondition, false);
                        if (drvResult != null)
                            FillBinData(drvResult.Row);
                        else
                            e.Cancel = true;
                    }
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void txtBin_KeyDown(object sender, KeyEventArgs e)
        {
            const string METHOD_NAME = This + ".txtBin_KeyDown()";
            try
            {
                if (e.KeyCode == Keys.F4)
                    btnBin_Click(null, null);
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void btnBin_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnBin_Click()";
            try
            {
                if (txtLocation.Text.Trim().Length == 0)
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                    txtBin.Text = string.Empty;
                    txtLocation.Focus();
                    return;
                }
                Hashtable htCondition = new Hashtable();
                htCondition.Add(MST_BINTable.LOCATIONID_FLD, Convert.ToInt32(txtLocation.Tag));
                DataRowView drvResult = FormControlComponents.OpenSearchForm(MST_BINTable.TABLE_NAME, MST_BINTable.CODE_FLD, txtBin.Text.Trim(), htCondition, true);
                if (drvResult != null)
                    FillBinData(drvResult.Row);
                else
                    txtTransNo.Focus();
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void dgrdData_ButtonClick(object sender, ColEventArgs e)
        {
            const string METHOD_NAME = This + ".dgrdData_ButtonClick()";
            try
            {
                if (!btnSave.Enabled) return;
                if (txtOrderNo.Text.Length == 0)
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                    txtOrderNo.Focus();
                    return;
                }
                string strColName = e.Column.DataColumn.DataField;
                Hashtable htbCondition = new Hashtable();
                htbCondition.Add(SO_SaleOrderDetailTable.SALEORDERDETAILID_FLD, Convert.ToInt32(txtOrderNo.Tag));
                DataRowView drwResult = null;
                if (dgrdData.AddNewMode == AddNewModeEnum.AddNewCurrent)
                    drwResult = FormControlComponents.OpenSearchForm(ItemView, strColName, dgrdData[dgrdData.Row, strColName].ToString(), htbCondition, true);
                else
                    drwResult = FormControlComponents.OpenSearchForm(ItemView, strColName, dgrdData.Columns[strColName].Value.ToString().Trim(), htbCondition, true);
                if (drwResult != null)
                {
                    dgrdData.EditActive = true;
                    FillItemDataToGrid(drwResult.Row);
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void dgrdData_BeforeColUpdate(object sender, BeforeColUpdateEventArgs e)
        {
            const string METHOD_NAME = This + ".dgrdData_BeforeColUpdate()";
            try
            {
                if (dgrdData.Splits[0].Locked)
                    return;
                if (txtOrderNo.Text.Length == 0)
                {
                    PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
                    txtOrderNo.Focus();
                    return;
                }
                if (e.Column.DataColumn.DataField == ITM_ProductTable.CODE_FLD ||
                    e.Column.DataColumn.DataField == ITM_ProductTable.DESCRIPTION_FLD)
                {
                    Hashtable htbCondition = new Hashtable();
                    string strColumnName = e.Column.DataColumn.DataField;
                    DataRowView drwResult = null;
                    htbCondition.Add(SO_SaleOrderDetailTable.SALEORDERDETAILID_FLD, Convert.ToInt32(txtOrderNo.Tag));
                    if (dgrdData.Columns[e.Column.DataColumn.DataField].Text.Trim() != string.Empty)
                    {
                        drwResult = FormControlComponents.OpenSearchForm(ItemView, strColumnName, dgrdData.Columns[e.Column.DataColumn.DataField].Value.ToString().Trim(), htbCondition, false);
                        if (drwResult != null)
                            e.Column.DataColumn.Tag = drwResult.Row;
                        else
                            e.Cancel = true;
                    }
                    else
                        e.Column.DataColumn.Tag = null;
                }
                else if (e.Column.DataColumn.DataField == SO_CommitInventoryDetailTable.PRICE_FLD)
                {
                    try
                    {
                        if (Convert.ToDecimal(dgrdData.Columns[e.Column.DataColumn.DataField].Text) <= 0)
                        {
                            string[] strMsg = new string[] { e.Column.DataColumn.Caption, "0" };
                            PCSMessageBox.Show(ErrorCode.MESSAGE_GREATER_THAN, MessageBoxIcon.Warning, strMsg);
                            e.Cancel = true;
                        }
                    }
                    catch
                    {
                        PCSMessageBox.Show(ErrorCode.MESSAGE_INVALID_NUMERIC, MessageBoxIcon.Warning);
                        e.Cancel = true;
                    }
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void dgrdData_AfterColUpdate(object sender, ColEventArgs e)
        {
            const string METHOD_NAME = This + ".dgrdData_AfterColUpdate()";
            try
            {
                dgrdData.EditActive = true;
                if (e.Column.DataColumn.DataField == ITM_ProductTable.CODE_FLD ||
                    e.Column.DataColumn.DataField == ITM_ProductTable.DESCRIPTION_FLD)
                {
                    FillItemDataToGrid((DataRow)e.Column.DataColumn.Tag);
                }
                else if (e.Column.DataColumn.DataField == SO_CommitInventoryDetailTable.PRICE_FLD)
                {
                    // update amount
                    decimal decQty = Convert.ToDecimal(dgrdData[dgrdData.Row, SO_CommitInventoryDetailTable.COMMITQUANTITY_FLD]);
                    decimal decPrice = Convert.ToDecimal(dgrdData[dgrdData.Row, SO_CommitInventoryDetailTable.PRICE_FLD]);
                    decimal decVAT = 0;
                    try
                    {
                        decVAT = Convert.ToDecimal(dgrdData[dgrdData.Row, SO_SaleOrderDetailTable.VATPERCENT_FLD]);
                    }
                    catch { }
                    dgrdData[dgrdData.Row, SO_SaleOrderDetailTable.NETAMOUNT_FLD] = decQty * decPrice;
                    dgrdData[dgrdData.Row, SO_SaleOrderDetailTable.VATAMOUNT_FLD] = (decQty * decPrice) + (decQty * decPrice * decVAT / 100);
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void txtBankAccount_Validating(object sender, CancelEventArgs e)
        {
            const string METHOD_NAME = This + ".txtBankAccount_Validating()";
            try
            {
                if (txtBankAccount.Modified && btnBank.Enabled)
                {
                    if (txtBankAccount.Text.Trim().Equals(string.Empty))
                    {
                        FillBankData(null);
                        return;
                    }

                    DataRowView drvResult = FormControlComponents.OpenSearchForm(MST_BankTable.TableName, MST_BankTable.BankAccount, txtBankAccount.Text.Trim(), null, false);
                    if (drvResult != null)
                    {
                        FillBankData(drvResult);
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }

        private void txtBankAccount_KeyDown(object sender, KeyEventArgs e)
        {
            if (btnBank.Enabled && e.KeyCode == Keys.F4)
            {
                btnBank_Click(null, null);
            }
        }

        private void btnBank_Click(object sender, EventArgs e)
        {
            const string METHOD_NAME = This + ".btnBank_Click()";
            try
            {
                DataRowView drvResult = FormControlComponents.OpenSearchForm(MST_BankTable.TableName, MST_BankTable.BankAccount, txtBankAccount.Text.Trim(), null, true);
                if (drvResult != null)
                {
                    FillBankData(drvResult);
                }
                else
                {
                    txtBankAccount.Focus();
                }
            }
            catch (PCSException ex)
            {
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                try
                {
                    Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
            catch (Exception ex)
            {
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR);
                try
                {
                    Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
                }
            }
        }
    }
}
